﻿using MC.RideEvent.Mvc.Models.Entity;
using MC.RideEvent.Mvc.Models.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MC.RideEvent.Mvc.Models.ViewModel;

namespace MC.RideEvent.Mvc.Controllers
{
    public class EventController : ControllerBase
    {
        private IEventRepository _eventRepo;
        private IWayRepository _wayRepo;

        public EventController(IEventRepository eventRepo, IWayRepository wayRepo)
        {
            _eventRepo = eventRepo;
            _wayRepo = wayRepo;
        }

        public ActionResult Index()
        {
            var listModel = _eventRepo.GetEvents();

            return View(listModel);
        }

        /// <summary>
        /// This method display the result
        /// of a research client action.
        /// </summary>
        /// <param name="text"></param>
        /// <param name="dateEvent"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Index(string text, DateTime dateEvent)
        {
            _eventRepo.ResearchEvents(text, dateEvent);
            return View();
        }

        public ActionResult Detail(int id)
        {
            var member = CurrentMember;
            DetailEventViewModel model = new DetailEventViewModel();
            model.Event = _eventRepo.GetEventById(id);
            model.Ways = _wayRepo.GetWaysByEventId(id);
            var listDriverWaysId = _wayRepo.GetWaysByDriverId(member.Id).Select(w => w.Id).ToArray();
            var listPassagerWaysId = _wayRepo.GetWaysByPassagerId(member.Id).Select(w => w.Id).ToArray();
            model.DriverWaysId = listDriverWaysId;
            model.PassagerWaysId = listPassagerWaysId;

            return View(model);
        }

        //public ActionResult _ResearchHome()
        //{
        //    return PartialView();
        //}

        //[HttpPost]
        //public ActionResult _ResearchHome(string textresearch, string dateresearch)
        //{
        //    return PartialView();
        //}

        [HttpPost]
        public ActionResult Research(string textresearch, string dateresearch)
        {
            List<EventViewModel> result = null;

            if(!string.IsNullOrEmpty(textresearch))
            {
                DateTime? d = null;
                if(!string.IsNullOrEmpty(dateresearch)) d = DateTime.Parse(dateresearch);

                result = _eventRepo.ResearchEvents(textresearch, d);
            }
            
            return View("Index", result);
        }

        #region Methods Services Json
        public ActionResult ListLastEventsJson()
        {
            var list = _eventRepo.GetLastEvents();
            return Json(list, JsonRequestBehavior.AllowGet);
        }

        #endregion
    }
}
